/*
 * 响应过滤器(拦截器)，统一返回体内容
 */

import {
  HttpStatus,
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';

@Injectable()
export class ResponseInterceptor<T>
  implements NestInterceptor<T, ApiResponseData<T>>
{
  intercept(
    context: ExecutionContext,
    next: CallHandler,
  ): Observable<ApiResponseData<T>> {
    return next.handle().pipe(
      map((data) => {
        return {
          path: context.switchToHttp().getRequest().url,
          code: 0,
          msg: '操作成功',
          data,
        };
      }),
    );
  }
}
